﻿@page "/System/UserRole"
@attribute [TabItemOption(Text = "用户角色")]
@using System.Diagnostics.CodeAnalysis;
@using BootstrapBlazorApp.Shared.Data.SYS;
@inject MessageService msg

<Table TItem="UserRoleDto" PageItemsSource="@PageItemsSource" AutoGenerateColumns="true"
       IsPagination="true" IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true" ShowExtendButtons="true" ShowLoading="true" ShowSearch="true"
       OnAddAsync="@OnAddAsync" OnEditAsync="@OnEditAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync"
       OnQueryAsync="@OnQueryAsync" ShowExportButton="true">
    <TableColumns>

    </TableColumns>
</Table>

@code {
    /// <summary>
    /// 当前Token信息
    /// </summary>
    [CascadingParameter]
    private ITokenContext? Context { get; set; }


    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };


    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
    }

    private static async Task<UserRoleDto> OnAddAsync()
    {
        // 模拟延时

        return new UserRoleDto() { };
    }

    private async Task<UserRoleDto> OnEditAsync(UserRoleDto item)
    {
       
        // 模拟延时
        return item;
    }

    private async Task<bool> OnSaveAsync(UserRoleDto item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Add)
        {
            item.Creater = Context.CurrentUser.Account;
            item.CreatedName = Context.CurrentUser.UserName;
            return await Global.ExecuteApiAsync(() => Request.System.AddUserRoleAsync(item));
        }
        else
        {
            item.Modifier = Context.CurrentUser.UserName;
            item.ModifiedName = Context.CurrentUser.UserName;
            return await Global.ExecuteApiAsync(() => Request.System.UpdateUserRoleAsync(item));
        }
    }

    private async Task<bool> OnDeleteAsync(IEnumerable<UserRoleDto> items)
    {
        return  await Global.ExecuteApiAsync(() => Request.System.DeleteMutiUserRoleAsync(items.ToList()));
    }

    private async Task<QueryData<UserRoleDto>> OnQueryAsync(QueryPageOptions options)
    {
        try
        {
            var model = await Request.System.QueryUserRoleAsync(QueryParameter.GetQueryParameter(options));
            return model.data;
        }
        catch (Exception ex)
        {
            return new QueryData<UserRoleDto>();
        }


    }

}
